<template>
  <div class="height_width-full">
    <div class="breadcrumb">
      <div class="breadcrumb-icon-box">
        <i class="el-icon-location-outline"></i>
      </div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path : '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path : '/asset-provider'}">供应商</el-breadcrumb-item>
        <el-breadcrumb-item>{{ curEditType }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-divider></el-divider>
    <div>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
            <el-form-item label="供应商名称" prop="title">
              <el-input v-model.trim="ruleForm.title" placeholder="请输入供应商名称"></el-input>
            </el-form-item>
            <el-form-item label="供应商类型" prop="providerTypeId">
              <el-select v-model="ruleForm.providerTypeId" placeholder="请选择供应商类型" clearable>
                <el-option :label="item.title" :value="item.id" v-for="(item,index) in providerTypeList"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="供应商联系人姓名" prop="providerName">
              <el-input v-model.trim="ruleForm.providerName" placeholder="请输入供应商联系人姓名"></el-input>
            </el-form-item>
            <el-form-item label="供应商移动电话" prop="providerPhone">
              <el-input v-model.trim="ruleForm.providerPhone" placeholder="请输入供应商移动电话"></el-input>
            </el-form-item>
            <el-form-item label="供应商地址" prop="providerAddress">
              <el-cascader
                size="large"
                :options="options"
                v-model="addressCode"
                @change="handleChange">
              </el-cascader>
              <el-input style="margin-top: 10px"
                        type="textarea"
                        :rows="2"
                        placeholder="请输入详细地址"
                        v-model="endAddress">
              </el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-switch
                v-model="ruleForm.status == 1"
                active-text="启用"
                inactive-text="禁用"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="formStatusChange">
              </el-switch>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :disabled="disabled" @click="submitForm('ruleForm')">立即保存</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>

import {regionData, CodeToText, TextToCode} from 'element-china-area-data'

export default {
  name: "EditPerson",
  data() {
    return {
      addressDetail: "",
      options: regionData,
      addressCode: "",
      startAddress: "",
      endAddress: "",
      providerTypeList: [],
      disabled: false,
      ruleForm: {
        id: null,
        title: '',
        providerTypeId: '',
        providerName: '',
        providerPhone: '',
        providerAddress: '',
        status: 1
      },
      rules: {
        title: [
          {required: true, message: '请输入供应商名称', trigger: 'blur'}, {
            max: 40, min: 2, message: "长度为2-40个字符", trigger: "blur"
          }
        ],
        providerTypeId: [
          {required: true, message: '请选择供应商类型', trigger: 'blur'},
        ],
        providerName: [
          {required: true, message: '请输入供应商联系人姓名', trigger: 'blur'}, {
            max: 20, min: 2, message: "长度为2-20个字符", trigger: "blur"
          }
        ],
        providerPhone: [
          {required: true, message: '请输入供应商移动电话', trigger: 'blur'},
          {required: true, pattern: /^1[3-9][\d]{9}$/, message: '请输入正确的电话号码', trigger: 'blur'}
        ],
        providerAddress: [
          {max: 2, min: 30, message: '请输入供应商地址', trigger: 'blur'}
        ]
      },
    };
  },
  created() {
    let data = this.Storage.Session.get("providerData");
    if (data.type) {
      this.curEditType = "添加供应商";
    } else {
      this.curEditType = "编辑供应商";
      let personData = this.Storage.Session.get("providerData").data;
      this.ruleForm = personData;
      console.log("ruleForm", this.ruleForm.providerAddress);
      let addressDetail = this.ruleForm.providerAddress;
      let addressArr = addressDetail.split("/");
      console.log("addressArr", addressArr);
      this.addressCode = TextToCode[addressArr[0]][addressArr[1]][addressArr[2]].code;
      console.log("addressCode", this.addressCode)
      addressArr.forEach((item, index) => {
        if (index > 2) {
          this.endAddress += item;
        }
      });
      console.log("endAddress", this.endAddress);
      let arr = [];
      arr.push(addressArr[0]);
      arr.push(addressArr[1]);
      arr.push(addressArr[2]);
      arr.forEach(item => this.startAddress += item + '/')
    }
    this.getProviderTypeList();
  },
  methods: {
    handleChange(value) {
      console.log("value", value);
      //我们选择地址后，selectedOptions 会保存对应的区域码，例如北京的区域码为'110000'
      //我们要取出区域码对应的汉字，就需要用CodeToText(区域码)进行输出
      value.forEach(item => this.startAddress += CodeToText[item] + '/')
    },
    formStatusChange(status) {
      this.form.status = status ? 1 : 2;
    },
    getProviderTypeList() {
      this.$get(this.$api.API_PROVIDER_TYPE_LIST).then(res => {
        if (res.data.code == 200) {
          this.providerTypeList = res.data.data;
        }
      })
    },
    toDisabled() {
      this.disabled = true;
      setTimeout(() => {
        this.disabled = false;
      }, 500)
    },
    getDepartList() {
      this.$get(this.$api.API_DEPART_LIST, {
        pageNum: 1,
        pageSize: 100
      }).then(res => {
        if (res.data.code == 200) {
          this.departList = res.data.data.list;
        }
      })
    },
    submitForm(formName) {
      this.toDisabled();
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.providerAddress = this.startAddress + this.endAddress;
          console.log(this.ruleForm.providerAddress);
          this.$put(this.$api.API_PROVIDER_SAVE, this.ruleForm).then(res => {
            if (res.data.code == 200) {
              this.$notify.success("保存成功");
              if (this.ruleForm.id) {
                if (this.ruleForm.id == this.Storage.Local.get("user").id) {
                  this.Storage.Local.set("user", this.ruleForm);
                  this.$store.commit("setUser", this.ruleForm);
                }
              }
              this.$router.push({
                name: "AssetProvider",
                path: "/asset-provider"
              })
            }
          })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
}
</script>

<style scoped>

</style>
